package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallCaller;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc  来电明细Action
 * @author yangzj
 * @datetime 2014年10月13日,下午7:45:53
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class CallDetailsAction extends PublicAction implements ModelDriven<MedicalCallCaller>{
	
	private static final long serialVersionUID = 5442139958077636127L;
	
	private MedicalCallCaller caller = new MedicalCallCaller();

	/**
	 * @desc 跳转到来电明细页面
	 * @return SUCCESS
	 * @author yangzj
	 * @datetime 2014年10月13日,下午7:33:25
	 */
	@Action(value = "call_details_init", results = {@Result(location = "call_details_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询来电明细
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月15日,下午1:14:31
	 */
	@Action(value = "call_details_view")
	public String view(){
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getTelService().findCallerByPage(admin, caller, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 导出全部来电明细
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月15日,下午4:08:51
	 */
	@Action(value = "call_details_export")
	public String export() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"来电明细.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getTelService().exportCaller(admin);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出当前页来电明细
	 * @return null
	 * @author yangzj
	 * @datetime 2014年10月15日,下午8:15:49
	 */
	@Action(value = "call_details_current_export")
	public String currentExport(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"来电明细.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getTelService().currentExportCaller(admin, caller.getIds());
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 话务任务-打开任务-查询通话记录
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月15日,上午4:03:10
	 */
	@Action(value = "customer_call_records_init", results = {@Result(location = "customer_call_records_init.jsp")})
	public String callRecordsInit() {
		return SUCCESS;
	}
	/**
	 * @desc 查询通话记录
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月15日,上午4:01:46
	 */
	@Action(value = "customer_call_records_view")
	public String callRecordsView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getTelService().findCallRecords(caller.getAnswerSeat(), admin);
		formatJSON(response, list);
		return null;
	}
	
	@Override
	public MedicalCallCaller getModel() {
		return caller;
	}
}
